let main;
let filter;
let receiver;
let tag = false;
/**
 * 开始广播监听扫码
 */
const start = () => {
    /* #ifdef APP-PLUS */
    main.registerReceiver(receiver, filter);
    /* #endif */
}

/**
 * 停止广播监听扫码
 */
const stop = () => {
    /* #ifdef APP-PLUS */
    main.unregisterReceiver(receiver);
    /* #endif */
}

/**
 * 定义广播
 */
const init = (onReceive) => {
	console.log('执行');
    /* #ifdef APP-PLUS */
    main = plus.android.runtimeMainActivity();
	console.log('执行main:',main);
    const IntentFilter = plus.android.importClass('android.content.IntentFilter');
	console.log('执行IntentFilter:',IntentFilter);
    filter = new IntentFilter();
	console.log('执行filter:',filter);
    // 更改为厂家提供的广播动作
    filter.addAction("com.rfid.SCAN");
    console.log('执行filter.addActionr:',filter.addAction("com.rfid.SCAN"));
    receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
        
        onReceive: function(context, intent) {
            plus.android.importClass(intent);
            
            // 更改为厂家提供的广播标签
            const code = intent.getStringExtra("scannerdata");
            console.log("Code:"+code);
            
            if (tag) return;
            tag = true;
            setTimeout(function() {
                tag = false;
            }, 150);
            
            onReceive && onReceive(code);
        }
    });
    /* #endif */
}

export const broadcastScan = {
    init,
    start,
    stop,
};
